﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class TemplateSettingEditViewModel : DialogViewModelBase, IDialogAware
    {
        private int _templateType = 0;
        private DelegateCommand<object> _okDialogCommand;
        private ITemplateSettingService _templateSettingService;
        public TemplateSettingEditViewModel(IDialogService dialogService, ITemplateSettingService tempServ) : base(dialogService)
        {
            Title = "编辑模板";
            _templateSettingService = tempServ;
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);
            //ComboxList.Add(new ComboBoxItemModel<SimpleIntModel> { Description = "内置的", IsEnable = true, SelectedModel = new SimpleIntModel { v1 = -1 } });
            ComboxList.Add(new ComboBoxItemModel<int> { Description = "规格书", IsEnable = true, SelectedModel = 0 });
            ComboxList.Add(new ComboBoxItemModel<int> { Description = "索引表", IsEnable = true, SelectedModel = 1 });
            ComboxList.Add(new ComboBoxItemModel<int> { Description = "材料表", IsEnable = true, SelectedModel = 2 });
            ComboxList.Add(new ComboBoxItemModel<int> { Description = "安装图", IsEnable = true, SelectedModel = 3 });
            ComboxList.Add(new ComboBoxItemModel<int> { Description = "接线相关", IsEnable = true, SelectedModel = 4 });
            //this.SelectedModel = ComboxList[0].SelectedModel;
            IOTypeList.Add(new ComboBoxItemModel<int> { Description = "N/A", IsEnable = true, SelectedModel = -1 });
            IOTypeList.Add(new ComboBoxItemModel<int> { Description = "输入", IsEnable = true, SelectedModel = 0 });
            IOTypeList.Add(new ComboBoxItemModel<int> { Description = "输出", IsEnable = true, SelectedModel = 1 });
            //this.SelectedIOType = IOTypeList[0].SelectedModel;

            ReportID = parameters.GetValue<string>("ReportID");
            TemplateName = parameters.GetValue<string>("Name");
            Parameters = parameters.GetValue<string>("Parameters");

            SelectedIOType = IOTypeList.FirstOrDefault(x => x.SelectedModel == parameters.GetValue<int>("InOrOut")).SelectedModel;
            SelectedTemplateType = ComboxList.FirstOrDefault(x => (TemplateType)x.SelectedModel == Enum.Parse<TemplateType>(parameters.GetValue<string>("Type"))).SelectedModel;
            if (SelectedTemplateType == 0)
            {
                SelectedIOType = IOTypeList[0].SelectedModel;
            }
            Description = parameters.GetValue<string>("Description");
            GenerateFunc = parameters.GetValue<string>("Generatefunc");
            SQL = parameters.GetValue<string>("Sql");
            TemplatePath = GlobalObject.CurrentProject.ProjName; //parameters.GetValue<string>("TemplatePath");
        }

        public ObservableCollection<ComboBoxItemModel<int>> ComboxList { get; set; } = new ObservableCollection<ComboBoxItemModel<int>>();
        private Visibility _NeedShow;

        public Visibility NeedShow
        {
            get { return _NeedShow; }
            set
            {
                SetProperty(ref _NeedShow, value);

            }
        }

        private int _selectedTemplateType;
        public int SelectedTemplateType
        {
            get { return _selectedTemplateType; }
            set
            {
                SetProperty(ref _selectedTemplateType, value);
                if (_selectedTemplateType == 0)
                {
                    NeedShow = Visibility.Collapsed;
                }
                else
                {
                    NeedShow = Visibility.Visible;
                }
            }
        }
        public ObservableCollection<ComboBoxItemModel<int>> IOTypeList { get; set; } = new ObservableCollection<ComboBoxItemModel<int>>();

        private int _selectedIOType;
        public int SelectedIOType
        {
            get { return _selectedIOType; }
            set
            {
                SetProperty(ref _selectedIOType, value);

            }
        }
        private string _reportID = "";
        public string ReportID
        {
            get { return _reportID; }
            set { SetProperty(ref _reportID, value); }
        }
        private string _templateName = "";
        public string TemplateName
        {
            get { return _templateName; }
            set { SetProperty(ref _templateName, value); }
        }

        private string _generateFunc = "";
        public string GenerateFunc
        {
            get { return _generateFunc; }
            set { SetProperty(ref _generateFunc, value); }
        }

        private string _templatePath = "";
        public string TemplatePath
        {
            get { return _templatePath; }
            set { SetProperty(ref _templatePath, value); }
        }
        private string _parameters = "";
        public string Parameters
        {
            get { return _parameters; }
            set { SetProperty(ref _parameters, value); }
        }
        private string _description = "";
        public string Description
        {
            get { return _description; }
            set { SetProperty(ref _description, value); }
        }

        private string _sql = "";
        public string SQL
        {
            get { return _sql; }
            set { SetProperty(ref _sql, value); }
        }



        public ICommand OkDialogCommand
        {
            get
            {
                if (_okDialogCommand != null) return _okDialogCommand;
                _okDialogCommand = new DelegateCommand<object>(onOkDialogCommand);
                return _okDialogCommand;
            }
        }


        private async void onOkDialogCommand(object parameter)
        {
            string proId = GlobalObject.CurrentProject.ProjId.ToString();

            if (_selectedTemplateType == null)
            {
                Dialog.ShowAltert("您的选择不规范，请选择新建模板类型。");
                return;
            }
            if (_selectedTemplateType.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的选择不规范，请选择新建模板类型。");
                return;
            }
            if (_selectedIOType == null)
            {
                Dialog.ShowAltert("您的选择不规范，请选择新建输入/输出。");
                return;
            }
            if (_selectedIOType.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的选择不规范，请选择新建输入/输出。");
                return;
            }
            if (_templateName.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的输入不规范，模板名称不能为空。");
                return;
            }
            if (_templatePath.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的输入不规范，模板路径不能为空。");
                return;
            }
            try
            {
                Report report = new Report
                {
                    ReportId = Guid.Parse(_reportID),
                    Description = _description,
                    GenerateFunc = GenerateFunc,
                    Name = _templateName,
                    SQL = _sql,
                    TemplatePath = _templatePath,
                    Type = (TemplateType)_selectedTemplateType,
                    Parameters = _parameters,
                    InOrOut = _selectedIOType
                };

                //ITemplateSettingService templateSettingService = _templateType == 1 ? _importUtilityService : _specOutputService;
                var r = await _templateSettingService.UpdateTemplateAsync(proId, report);
                if (r != null)
                {
                    Dialog.ShowAltert("上传文件至远程服务器成功！！！");
                    RaiseRequestClose(new DialogResult(ButtonResult.OK));
                }
                else
                {
                    RaiseRequestClose(new DialogResult(ButtonResult.None));
                }
            }
            catch (Exception x)
            {
                var xMessage = $"上传文件至远程服务器失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
